


package jnpf.property.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.property.entity.ZcglPropertyMeterEntity;
import jnpf.property.model.zcglpropertymeter.ZcglPropertyMeterForm;
import jnpf.property.model.zcglpropertymeter.ZcglPropertyMeterInfoVO;
import jnpf.property.model.zcglpropertymeter.ZcglPropertyMeterListVO;
import jnpf.property.model.zcglpropertymeter.ZcglPropertyMeterPagination;
import jnpf.property.service.ZcglPropertyMeterService;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * zcglPropertyMeter
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-03
 */
@Slf4j
@RestController
@Api(tags = "zcglPropertyMeter", value = "example")
@RequestMapping("/api/example/ZcglPropertyMeter")
public class ZcglPropertyMeterController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglPropertyMeterService zcglPropertyMeterService;




    /**
     * 列表
     *
     * @param zcglPropertyMeterPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglPropertyMeterPagination zcglPropertyMeterPagination) throws IOException {
        List<ZcglPropertyMeterEntity> list = zcglPropertyMeterService.getList(zcglPropertyMeterPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglPropertyMeterListVO> listVO = JsonUtil.getJsonToList(list, ZcglPropertyMeterListVO.class);
        for (ZcglPropertyMeterListVO zcglPropertyMeterVO : listVO) {
            zcglPropertyMeterVO.setProject_name(zcglPropertyMeterVO.getProject());

            zcglPropertyMeterVO.setZcId_name(zcglPropertyMeterVO.getZcId());

            zcglPropertyMeterVO.setEquipmentName_name(zcglPropertyMeterVO.getEquipmentName());

            zcglPropertyMeterVO.setPrice_name(zcglPropertyMeterVO.getPrice());

            zcglPropertyMeterVO.setEquipmentPower_name(zcglPropertyMeterVO.getEquipmentPower());

            zcglPropertyMeterVO.setPreviousReading_name(zcglPropertyMeterVO.getPreviousReading());

            zcglPropertyMeterVO.setPreviousTime_name(zcglPropertyMeterVO.getPreviousTime());

            zcglPropertyMeterVO.setCurrentReading_name(zcglPropertyMeterVO.getCurrentReading());

            zcglPropertyMeterVO.setCurrentUsage_name(zcglPropertyMeterVO.getCurrentUsage());

            zcglPropertyMeterVO.setCurrentCost_name(zcglPropertyMeterVO.getCurrentCost());

            zcglPropertyMeterVO.setCurrentTime_name(zcglPropertyMeterVO.getCurrentTime());

            zcglPropertyMeterVO.setFileAddress_name(generaterSwapUtil.getFileNameInJson(zcglPropertyMeterVO.getFileAddress()));

            zcglPropertyMeterVO.setRemarks_name(zcglPropertyMeterVO.getRemarks());

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglPropertyMeterPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcglPropertyMeterForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglPropertyMeterForm zcglPropertyMeterForm) throws DataException {

        ActionResult actionResult = zcglPropertyMeterService.saveMeter(zcglPropertyMeterForm);
        return actionResult;
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglPropertyMeterInfoVO> info(@PathVariable("id") String id) {
        ZcglPropertyMeterEntity entity = zcglPropertyMeterService.getInfo(id);
        ZcglPropertyMeterInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglPropertyMeterInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglPropertyMeterInfoVO> detailInfo(@PathVariable("id") String id) {
        ZcglPropertyMeterEntity entity = zcglPropertyMeterService.getInfo(id);
        ZcglPropertyMeterInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglPropertyMeterInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setProject(vo.getProject());

        vo.setZcId(vo.getZcId());

        vo.setEquipmentName(vo.getEquipmentName());

        vo.setPrice(vo.getPrice());

        vo.setEquipmentPower(vo.getEquipmentPower());

        vo.setPreviousReading(vo.getPreviousReading());

        vo.setPreviousTime(vo.getPreviousTime());

        vo.setCurrentReading(vo.getCurrentReading());

        vo.setCurrentUsage(vo.getCurrentUsage());

        vo.setCurrentCost(vo.getCurrentCost());

        vo.setCurrentTime(vo.getCurrentTime());

        vo.setFileAddress(generaterSwapUtil.getFileNameInJson(vo.getFileAddress()));

        vo.setRemarks(vo.getRemarks());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param zcglPropertyMeterForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglPropertyMeterForm zcglPropertyMeterForm) throws DataException {
        boolean b = zcglPropertyMeterService.checkForm(zcglPropertyMeterForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglPropertyMeterEntity entity = zcglPropertyMeterService.getInfo(id);
        if (entity != null) {
            ZcglPropertyMeterEntity subentity = JsonUtil.getJsonToBean(zcglPropertyMeterForm, ZcglPropertyMeterEntity.class);
            boolean b1 = zcglPropertyMeterService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglPropertyMeterEntity entity = zcglPropertyMeterService.getInfo(id);
        if (entity != null) {
            zcglPropertyMeterService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
